package com.tanhua.service;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.dao.AdminMapper;
import com.tanhua.domain.Admin;
import com.tanhua.exception.BusinessException;
import com.tanhua.interceptor.AdminHolder;
import com.tanhua.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class AdminService {


    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    //制作返回一个验证码
    public ResponseEntity verification(String uuid, HttpServletResponse response) throws IOException {

        //hutool制作验证码
        LineCaptcha aaa = CaptchaUtil.createLineCaptcha(300, 100);
        //获取验证码的随机数
        String yzm = aaa.getCode();
        //将验证码存入到redis中
        stringRedisTemplate.opsForValue().set("manager_code_"+uuid,yzm,5, TimeUnit.MINUTES);

        //将验证码写获取
        aaa.write(response.getOutputStream());

        return ResponseEntity.ok(null);
    }

    public ResponseEntity login(String username, String password, String verificationCode, String uuid) {

        // 后台校验 (抛出验证码为空的信息)
        if(StrUtil.isEmpty(verificationCode) || StrUtil.isEmpty(uuid)){
            throw new BusinessException("验证码不能为空");
        }

        // 判断用户名密码为空
        if(StrUtil.isEmpty(username) || StrUtil.isEmpty(password)){
            throw new BusinessException("用户名或密码为空");
        }

        String yzm = stringRedisTemplate.opsForValue().get("manager_code_" + uuid);

        if(StrUtil.isEmpty(yzm) || !verificationCode.equals(yzm)){
            throw new BusinessException("验证码输入有误");
        }

        //带着用户名获取对象
        QueryWrapper<Admin> qw = new QueryWrapper();
        qw.eq("username",username);
        Admin admin = adminMapper.selectOne(qw);

        if(admin==null){
            throw new BusinessException("用户名不存在,请先去注册");
        }

        //需要将用户写在页面的密码进行加密和从数据库查询出来的用户密码做对比
        String md5Password = SecureUtil.md5(password);
        if(!md5Password.equals(admin.getPassword())){
            throw new BusinessException("密码错误");
        }

        // 以上内容都能跑通 --登录成功
        stringRedisTemplate.delete("manager_code_"+uuid);

        String token = JwtUtil.createToken(admin, "tanhua-manager");
        //loginUser--字符串json
        String json_user = JSON.toJSONString(admin);
        stringRedisTemplate.opsForValue().set("manager_token_"+token,json_user,1, TimeUnit.DAYS);

        // 设定返回值
        Map returnMap=new HashMap();
        returnMap.put("token",token);
        return ResponseEntity.ok(returnMap);

    }


    //从redis根据token获取对象
    public Admin tokenfromUser(String token) {
        String json = stringRedisTemplate.opsForValue().get("manager_token_" + token);
        //将json转换成对象
        Admin admin = JSON.parseObject(json, Admin.class);
        return admin;
    }

    public ResponseEntity logout() {
        //将redis中的token删除
        Admin admin = AdminHolder.getAdmin();
        stringRedisTemplate.delete("manager_token_"+admin.getToken());
        return ResponseEntity.ok(null);
    }


}
